﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Design;
using System.Windows.Forms;
using System.ComponentModel.Design;
using UIDesigner.Designers.Forms;
using UIDesigner.SurfaceProperties;
using System.Windows.Forms.Design;
using System.Diagnostics;

namespace UIDesigner.Editors
{
    public class PostActionEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }

        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            IWindowsFormsEditorService editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
            PostActions ePostActions = (PostActions)value;

            if (editorService != null)
            {
                CheckedListBox lstValues = new CheckedListBox();
                string strName;
                int nIndex;

                lstValues.BorderStyle = BorderStyle.None;
                lstValues.Height = 150;

                foreach (PostActions ePostAction in Enum.GetValues(typeof(PostActions)))
                {
                    strName = Enum.GetName(typeof(PostActions), ePostAction);

                    if (ePostAction != PostActions.None)
                    {
                        nIndex = lstValues.Items.Add(ePostAction);

                        if ((ePostActions & ePostAction) == ePostAction)
                        {
                            lstValues.SetItemChecked(nIndex, true);
                        }
                    }
                }

                editorService.DropDownControl(lstValues);

                ePostActions = PostActions.None;

                foreach (PostActions ePostAction in lstValues.CheckedItems)
                {
                    ePostActions |= ePostAction; 
                }

                // Return the new property value from the UI editor control
                return ePostActions;
            }
            else
            {
                Debugger.Break();
                return null;
            }
        }
    }
}
